package com.etc.result;

import lombok.Data;

import java.util.Collection;


/**
 * @Author : DDS
 * @Description : 统一返回结果集
 * @Date: 2019-11-28 15:12
 */
@Data
public class ResultBean<T> {


    private int code;
    private String message;
    private T data;

    private ResultBean() {

    }

    /**
     * 返回异常
     * @param code
     * @param message
     * @return
     */
    public static ResultBean error(int code, String message) {
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(code);
        resultBean.setMessage(message);
        return resultBean;
    }

    /**
     * 返回成功无参数
     * @return
     */
    public static ResultBean success() {
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(0);
        resultBean.setMessage("success");
        return resultBean;
    }

    /**
     * 返回成功带数据
     * @param data
     * @param <V>
     * @return
     */
    public static <V> ResultBean<V> success(Collection<V> data) {
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(0);
        resultBean.setMessage("success");
        resultBean.setData(data);
        return resultBean;
    }
    /**
     * 返回成功带数据
     * @param data
     * @param <V>
     * @return
     */
    public static <V> ResultBean<V> success(Object data) {
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(0);
        resultBean.setMessage("success");
        resultBean.setData(data);
        return resultBean;
    }
}
